package cn.demengsite.service.impl;

import cn.demengsite.dao.ProductDAO;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.Product;
import cn.demengsite.model.ProductImageAttr;
import cn.demengsite.service.InventoryService;
import cn.demengsite.utils.ProductUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cn.demengsite.base.ProductConfig.productColorMap;
import static cn.demengsite.base.ProductConfig.productNameMap;
import static cn.demengsite.base.ProductConfig.productSizeMap;

@Repository
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private ProductDAO productDAO;

    @Override
    public BaseInfo getAllProduct() {

        List<Product> products = productDAO.getAllProduct();
        //开始过滤
        products.stream();

        return new BaseInfo("0","dd",products);
    }

    @Override
    public BaseInfo getAllProductGroupByProductId() {

        List<Product> products = productDAO.getAllProductGroupByProductId();
        //开始过滤
        products.stream();

        return new BaseInfo("0","dd",products);
    }

    @Override
    public BaseInfo getAllProductByType(String typeCode) {

        List<Product> products = productDAO.getAllProduct();

       List<Product> typeProduct = products.stream().filter(product -> !product.getBarCode().equals("9998")&&(product.getBarCode().substring(4,6)).equals(typeCode)).collect(Collectors.toList());

        return new BaseInfo("0","dd",typeProduct);
    }

    @Override
    public BaseInfo insertProduct(List<Product> products) {

        if(products.get(0).getBarCode()==null||products.get(0).getBarCode().equals("")){

            int count = 0;
            for (Product product : products) {

                String barCode = product.getProductId()+product.getColorCode()+product.getSizeCode();
                product.setBarCode(barCode);
                Product exitProduct = productDAO.getProductByBarCode(barCode);
                if(exitProduct!=null){
                    //已有的，只需要更新就可以了
                    productDAO.updateProductNum(barCode,product.getProductNum());
                    count++;
                }else {
                    //需要额外插入的
                    productDAO.insertProduct(product);
                }
            }

            System.out.println(count);

        }

        return new BaseInfo("0","成功插入");
    }

    @Override
    public BaseInfo updateProduct(Product product) {

        productDAO.updateProductByBarCode(product);

        return new BaseInfo("0","OK");
    }

    @Override
    public BaseInfo updateProductByProductId(Product product) {
        productDAO.updateProductByProductId(product);

        return new BaseInfo("0","OK");
    }

    @Override
    public BaseInfo updateProductImage(String productId, int attrType, String attrUrl) {
        ProductImageAttr imageAttr = new ProductImageAttr();
        imageAttr.setAttrType(attrType);
        imageAttr.setAttrUrl(attrUrl);
        imageAttr.setProductId(productId);
        imageAttr.setOrderNum(0);
        productDAO.newProductImage(imageAttr);
        return new BaseInfo("0","OK");
    }


    @Override
    public BaseInfo setSalePrice(String styleCode,int saleType) {

        List<Product> products = (List<Product>) getAllProductByType(styleCode).getObject();
        //然后根据条码循环写入
        double discount =0.0;
        switch (saleType) {
            case 1:discount=0.28;break;
            case 2:discount=0.3;break;
            case 3:discount=0.38;break;
        }
        for (Product product : products) {
            productDAO.updateProductSalePriceByDiscount(product.getBarCode(),discount);
        }
        return null;
    }
}
